package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpStatus;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.entity.lindi.po.LindiPurchaseApplicationFile;
import com.minimalist.common.entity.lindi.vo.purchaseapplicationfile.LindiPurchaseApplicationFileVO;
import com.minimalist.common.feign.client.basic.FileFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.lindi.mapper.LindiPurchaseApplicationFileMapper;
import com.minimalist.lindi.service.LindiPurchaseApplicationFileService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标交易平台_购买申请文件 服务层实现。
 *
 * @author 11987
 * @since 2025-07-01
 */
@Service
public class LindiPurchaseApplicationFileServiceImpl extends ServiceImpl<LindiPurchaseApplicationFileMapper, LindiPurchaseApplicationFile> implements LindiPurchaseApplicationFileService{

    private static final Logger logger = LoggerFactory.getLogger(LindiPurchaseApplicationFileServiceImpl.class);

    @Autowired
    private LindiPurchaseApplicationFileMapper mapper;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Override
    public List<LindiPurchaseApplicationFileVO> getFileListByPurchaseApplicationId(Long purchaseApplicationId){
        List<LindiPurchaseApplicationFile> fileList = mapper.selectFileListByPurchaseApplicationId(purchaseApplicationId);
        return BeanUtil.copyToList(fileList, LindiPurchaseApplicationFileVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAddFiles(List<LindiPurchaseApplicationFileVO> fileList){
        if (fileList != null && !fileList.isEmpty()){
            List<LindiPurchaseApplicationFile> entityList = BeanUtil.copyToList(fileList, LindiPurchaseApplicationFile.class);
            saveBatch(entityList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFilesByPurchaseApplicationId(Long purchaseApplicationId){
        mapper.deleteFilesByPurchaseApplicationId(purchaseApplicationId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFileByFileId(Long fileId){
        // 先查询文件记录获取实际文件ID
        LindiPurchaseApplicationFile fileRecord = mapper.selectFileByFileId(fileId);
        Assert.notNull(fileRecord, () -> new BusinessException("文件记录不存在"));

        // 删除实际文件
        try{
            R<Void> result = fileFeignClient.deleteFile(fileRecord.getFileId());
            if (result.getCode() != HttpStatus.HTTP_OK){
                logger.warn("删除实际文件失败，文件ID: {}, 错误信息: {}", fileRecord.getFileId(), result.getMessage());
            }
        } catch (Exception e){
            logger.warn("删除实际文件异常，文件ID: {}, 异常信息: {}", fileRecord.getFileId(), e.getMessage());
        }

        // 删除数据库记录
        mapper.deleteFileByFileId(fileId);
    }
}
